package com.luoran.bartalks.service.impl;

import java.util.List;

import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.luoran.bartalks.dao.IBtMaterielDao;
import com.luoran.bartalks.dao.IBtPackingListDao;
import com.luoran.bartalks.dao.IBtPackingListMaterielDao;
import com.luoran.bartalks.entity.BaseInfo;
import com.luoran.bartalks.entity.biz.BtMateriel;
import com.luoran.bartalks.entity.biz.BtPackingList;
import com.luoran.bartalks.entity.biz.BtPackingListMateriel;
import com.luoran.bartalks.service.IBtPackingListService;

@Service
public class BtPackingListService implements IBtPackingListService {

	@Autowired
	private IBtPackingListDao packingListDao;
	@Autowired
	private IBtMaterielDao materielDao;
	@Autowired
	private IBtPackingListMaterielDao packingListMaterielDao;

	@Override
	public PageQuery<? extends BaseInfo> queryList(PageQuery<? extends BaseInfo> pageQuery) {
		packingListDao.queryPage(pageQuery);
		return pageQuery;
	}

	// 插入一条箱单数据
	@Transactional
	@Override
	public String addPackingList(BtPackingList packingList) {
		packingListDao.insertTemplate(packingList);
		return packingList.getId();
	}

	// 主键查询
	@Override
	public BtPackingList lookPackingList(String id) {

		return packingListDao.single(id);
	}

	// 根据主键更新
	@Transactional
	@Override
	public int updatePackingList(BtPackingList packingList) {
		return packingListDao.updateTemplateById(packingList);
	}

	// 根据主键更新状态,逻辑删除
	@Override
	public int deletePackingList(BtPackingList packingList) {
		return packingListDao.updateTemplateById(packingList);
	}

	// 根据计划id查询是否有箱单
	@Override
	public List<BtPackingList> findPackingListByPlanId(String planId) {
		return packingListDao.findPackingListByPlanId(planId);
	}

	@Transactional
	@Override
	public void leadingIn(BtPackingList packingList, List<BtMateriel> materiels,
			List<BtPackingListMateriel> packingListMateriels) {
		try {
			packingListDao.insertTemplate(packingList);
			materielDao.insertBatch(materiels);
			packingListMaterielDao.insertBatch(packingListMateriels);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("leadingIn 插入失败");
		}
	}

	@Override
	public int checkByPackingListIdAndPlanId(String PackingListId, String planId, String companyId) {
		return packingListDao.checkByPackingListIdAndPlanId(PackingListId, planId, companyId);
	}

}
